package view;
 
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import controller.Controladora;
 
@SuppressWarnings("serial")
public class TabbedPane extends JPanel {
	
	private Image image;
	private JTabbedPane tabbedPane = new JTabbedPane();
	
    public TabbedPane(Controladora controladora) {
        super(new BorderLayout());
        this.image=new ImageIcon("background.jpg").getImage();
        ImageIcon icon=null;
        
        final URL circuitIconPath = this.getClass().getResource("/view/imagenes/icons/cono.png");
		java.awt.Image circuitImage = getToolkit().createImage(circuitIconPath);
		icon = new ImageIcon(circuitImage);
        
        PlotCircuit circuit = new PlotCircuit(controladora);
        tabbedPane.addTab("1. Circuit Trace", icon, circuit, "Show circuit trace");
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
        
        final URL tableIconPath = this.getClass().getResource("/view/imagenes/icons/lapstatistics.png");
		java.awt.Image tableImage = getToolkit().createImage(tableIconPath);
		icon = new ImageIcon(tableImage);
		
        final PlotTable table = new PlotTable(controladora);
        tabbedPane.addTab("2. Laps Statistics", icon, table, "Show statistics");
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);        
        
        final URL speedPath = this.getClass().getResource("/view/imagenes/icons/speedplot.png");
		java.awt.Image speedImage = getToolkit().createImage(speedPath);
		icon = new ImageIcon(speedImage);
        
        PlotStatistics speed = new PlotStatistics(controladora);
        tabbedPane.addTab("3. Speed Plot", icon, speed, "Show speed plot");
        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
        
        final URL summaryPath = this.getClass().getResource("/view/imagenes/icons/summary.png");
		java.awt.Image summaryImage = getToolkit().createImage(summaryPath);
		icon = new ImageIcon(summaryImage);
        
        final PlotSummary tableSummmary = new PlotSummary(controladora);
        tabbedPane.addTab("4. Summary", icon, tableSummmary, "Show summary table");
        tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);    
         
        //Add the tabbed pane to this panel.
        add(tabbedPane, BorderLayout.CENTER);

        //The following line enables to use scrolling tabs.
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        
        
        ChangeListener changeListener = new ChangeListener() {
            public void stateChanged(ChangeEvent changeEvent) {
              JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
              int index = sourceTabbedPane.getSelectedIndex();
              
              if ("2. Laps Statistics".equals(sourceTabbedPane.getTitleAt(index))){
            	  table.updateTable(); 
              }
              if ("4. Summary".equals(sourceTabbedPane.getTitleAt(index))){
            	  tableSummmary.updateTable();
              }
            }
          };
        tabbedPane.addChangeListener(changeListener);
    }
    
    /**
     * Reseteja el circuit
     */
    public void resetCircuit() {
    	((PlotCircuit)tabbedPane.getComponent(0)).setSortidaFixada(false);
    	((PlotCircuit)tabbedPane.getComponent(0)).setxPunt(-10000);
    	((PlotCircuit)tabbedPane.getComponent(0)).setyPunt(-10000);
    }
    
    /**
     * Inicialitza el circuit
     */
    public void initPlotCircuit() {
    	((PlotCircuit)tabbedPane.getComponent(0)).setInit(true);
    	((PlotCircuit)tabbedPane.getComponent(0)).loadCircuit();
    }
     
    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }
     
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = TabbedPane.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    
    @Override
    protected void paintComponent(Graphics g) {
    	g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }

	/**
	 * @return the tabbedPane
	 */
	public JTabbedPane getTabbedPane() {
		return tabbedPane;
	}
    
    
}
